<template>
  <div class="login-box">
    <div class="content">
      <!-- 左侧log -->
      <div class="contentLeft">
        <h1>物美智能 - 物联美好生活</h1>
        <h2>wumei smart open source living iot platform</h2>
        <div class="btnsBox">
          <span>登录方式</span>
          <div class="btn btnWX">微信</div>
          <div class="btn btnQQ">QQ</div>
          <div class="btn btnZFB">支付宝</div>
        </div>
        <div class="Bigbtns">
          <div class="btn">返回官网</div>
          <div class="btn">查看文档</div>
          <div class="btn loginbtn" @click="goLogin">账号登录</div>
        </div>
        <div class="adminBox">
          <div>演 示<br />账 号</div>
          <ul>
            <li>受限管理<span>wumei</span><span>123456</span></li>
            <li>租户账号<span>wumei-ti</span><span>123456</span></li>
            <li>普通用户<span>wumei-u1</span><span>123456</span></li>
          </ul>
        </div>
      </div>
      <!-- 右侧登陆框 -->
      <div class="contentRight">
        <h3 class="title">注册账号</h3>
        <!-- 表单 -->
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
          <el-form-item prop="account">
            <el-input
              autofocus
              v-model.trim="ruleForm.account"
              prefix-icon="el-icon-s-custom"
              placeholder="账号"
            ></el-input>
          </el-form-item>
          <el-form-item prop="number">
            <el-input
              autofocus
              v-model.trim="ruleForm.number"
              prefix-icon="el-icon-s-custom"
              placeholder="手机号码"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              show-password
              v-model.trim="ruleForm.password"
              prefix-icon="el-icon-s-goods"
              placeholder="密码"
            ></el-input>
          </el-form-item>
          <el-form-item prop="confirm">
            <el-input
              show-password
              v-model.trim="ruleForm.confirm"
              prefix-icon="el-icon-s-goods"
              placeholder="确认密码"
            ></el-input>
          </el-form-item>
          <el-form-item prop="checkPass">
            <div class="inputbox">
              <input
                type="text"
                v-model.trim="ruleForm.logincode"
                placeholder="验证码"
              />
            </div>
            <div class="image">
              <img :src="imgBase64" alt="" @click="getcaptchaImage" />
            </div>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" :loading="confirmLoading" @click="submit">
              注册
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import _ from "@/assets/utils";
export default {
  name: "Register",
  data() {
    // 校验账号规则
    const checkAccount = (_, value, callback) => {
      if (value.length === 0) {
        callback(new Error("账号不能为空!"));
        return;
      }
      let reg1 = /^([a-zA-z0-9\-.]+)/g;
      if (!reg1.test(value)) {
        callback(new Error("账号格式不正确!"));
        return;
      }
      callback();
    };
    //校验密码规则
    const checkPassword = (_, value, callback) => {
      if (value.length === 0) {
        callback(new Error("密码不能为空!"));
        return;
      }
      let reg = /^\d+$/;
      if (!reg.test(value)) {
        callback(new Error("密码格式不正确!"));
        return;
      }
      callback();
    };
    // 校验手机号
    const checkNumber = (_, value, callback) => {
      if (value.length === 0) {
        callback(new Error("手机号不能为空!"));
        return;
      }
      let reg = /^(?:(?:\+|00)86)?1\d{10}$/;
      if (!reg.test(value)) {
        callback(new Error("手机号格式不正确!"));
        return;
      }
      callback();
    };
    // 校验确认密码
    const checkConfirm = (_, value, callback) => {
      let password = this.ruleForm.password;
      if (value === password) {
        callback();
      } else {
        callback(new Error("两次密码不一致!"));
      }
    };
    return {
      imgBase64: null,
      //表单校验
      ruleForm: {
        account: "", //账号
        password: "", //密码
        number: "",
        confirm: "",
        logincode: null,
        uuid: null,
      },
      //表单校验规则
      rules: {
        account: [
          { validator: checkAccount, trigger: "blur" },
          { min: 5, max: 15, message: "长度在5-15个字符" },
        ],
        password: [
          { validator: checkPassword, trigger: "blur" },
          { min: 6, max: 16, message: "长度在6-16个字符" },
        ],
        number: [{ validator: checkNumber, trigger: "blur" }],
        confirm: [{ validator: checkConfirm, trigger: "blur" }],
      },
      confirmLoading: false, //登录按钮的loading状态
    };
  },
  created() {
    this.getcaptchaImage();
  },
  methods: {
    //  获取验证码
    async getcaptchaImage() {
      try {
        let { img, code, uuid } = await this.$http.getVerificationCode();
        if (+code === 200) {
          this.ruleForm.uuid = uuid;
          this.imgBase64 = "data:image/gif;base64," + img;
        } else {
          this.$message.error("验证码获取失败");
        }
      } catch (_) {}
    },
    // 提交注册
    async submit() {
      await this.$refs.ruleForm.validate();
      let { _, logincode, confirm, password, number, account, uuid } =
        this.ruleForm;
      let { code, msg } = await this.$http.register(
        _,
        logincode,
        confirm,
        password,
        number,
        account,
        uuid
      );
      if (+code === 200) {
        this.$message.success("注册成功");
        this.$router.push("/login");
      } else {
        this.$message.error(msg || "操作失败！");
      }
    },

    // 切换登录
    goLogin() {
      this.$router.push("/login");
    },
  },
};
</script>

<style lang="less" scoped>
.login-box {
  background: linear-gradient(303deg, #48c6ef 10%, #6f86d6 80%);
  height: 100%;
  overflow: auto;
  padding-top: 100px;
}
.content {
  display: flex;
  width: 1200px;
  margin: auto;
}
.contentLeft {
  width: 710px;
  color: #fff;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  align-items: center;
  h1,
  h2 {
    color: #fff;
  }
  h1 {
    font-size: 46px;
  }
  h2 {
    font-size: 26px;
  }
}
.contentRight {
  width: 370px;
  height: 426px;
  box-shadow: 0 20px 30px 0 rgb(0 0 0 / 10%);
  border-radius: 10px;
  background-color: hsla(0, 0%, 98%, 0.8);
  border: 1px solid #fff;
  padding: 25px 25px 5px 25px;
  margin: 0 auto;
  max-width: 370px;
  margin-bottom: 100px;
}
.btnsBox {
  display: flex;
  margin: 30px 0px 20px 0px;
  width: auto;
  span {
    margin-right: 10px;
    font-size: 16px;
  }
  line-height: 28px;
  .btn {
    width: 70px;
    height: 28px;
    border: 1px solid #fff;
    color: #fff;
    font-size: 14px;
    line-height: 28px;
    text-align: center;
    border-radius: 3px;
  }

  .btnWX {
    background-color: #89e7b3;
  }
  .btnQQ {
    background-color: #ffa4a4;
    margin: 0px 8px;
  }
  .btnZFB {
    background-color: #8cc8ff;
  }
}
.Bigbtns {
  display: flex;
  .btn {
    width: 95px;
    height: 40px;
    font-size: 14px;
    color: #fff;
    line-height: 40px;
    text-align: center;
    border-radius: 5px;
  }
  .btn:first-child {
    background: #ffba00;
  }
  .btn:nth-child(2) {
    background: #13ce66;
    margin: 0px 8px;
  }
  .btn:nth-child(3) {
    background: #fff;
    color: #000;
    cursor: pointer;
  }
}
.adminBox {
  display: flex;
  border: 1px solid rgb(255, 255, 255);
  margin-top: 30px;
  width: 300px;
  display: flex;
  div {
    font-size: 18px;
    width: 75px;
    padding: 15px;
    line-height: 28px;
    font-weight: bold;
    box-sizing: border-box;
    background: rgb(245, 108, 108);
    margin: 2px;
  }
  ul {
    flex: 1;
  }
  li {
    font-size: 14px;
    height: 26px;
    line-height: 26px;
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    justify-content: space-between;
    padding: 0px 15px;
  }
}

.el-form-item {
  display: flex;
  //   height: 30px;
  line-height: 30px;
  //   margin-bottom: 7px;
  //   overflow: hidden;
  .el-input {
    height: 30px;
  }
  button {
    width: 370px;
  }
}

h3.title {
  font-size: 28px;
  text-align: center;
  margin-bottom: 20px;
  font-weight: 500;
}
.el-input {
  width: 325px;
}
.inputbox {
  width: 45%;
  height: 35px;
  display: inline-block;
  margin-right: 50px;
  position: relative;
  input {
    height: 100%;
    position: absolute;
    top: 10px;
    border: none;
    padding-left: 10px;
  }
}
.image {
  width: 101px;
  height: 38px;
  position: absolute;
  left: 200px;
  top: 10px;
  img {
    width: 100%;
    height: 100%;
  }
}
</style>